package com.hex.liyue.usermodule.presenter;

import android.text.TextUtils;

import com.hex.liyue.LiYueApplication;
import com.hex.liyue.commonmodule.bean.BaseBean;
import com.hex.liyue.commonmodule.listener.UploadFileListener;
import com.hex.liyue.commonmodule.manager.HttpManager;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.net.ResponseListener;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.OssUtils;
import com.hex.liyue.commonmodule.utils.T;
import com.hex.liyue.usermodule.bean.MyBankInfoResultBean;

/**
 * Created by WJ on 2016/6/27.
 */
public class UserBankPresenter {
    private String urlIdFront, urlIdBack, urlBankFront, urlBankBack;
    private IUserBankView iUserBankView;

    public UserBankPresenter(IUserBankView iUserBankView) {
        this.iUserBankView = iUserBankView;
    }

    /**
     * 更新/添加 银行卡信息
     *
     * @param bank_id
     */
    public void savaUserBankInfo(final String bank_id) {
        if (TextUtils.isEmpty(iUserBankView.getUsername())) {
            iUserBankView.showT("请输入户名!");
            return;
        }
        if (TextUtils.isEmpty(iUserBankView.getBankNumber())) {
            iUserBankView.showT("请输入银行卡号!");
            return;
        }
        if (TextUtils.isEmpty(iUserBankView.getBank())) {
            iUserBankView.showT("请输入所属银行!");
            return;
        }
        if (iUserBankView.getIdFront() == null || !iUserBankView.getIdFront().exists()) {
            if (TextUtils.isEmpty(iUserBankView.getIdPositive())) {
                iUserBankView.showT("请上传身份证正面照片!");
                return;
            }
        }
        if (iUserBankView.getIdBack() == null || !iUserBankView.getIdBack().exists()) {
            if (TextUtils.isEmpty(iUserBankView.getIdBacks())) {
                iUserBankView.showT("请上传身份证反面照片!");
                return;
            }
        }
        if (iUserBankView.getBankFront() == null || !iUserBankView.getBankFront().exists()) {
            if (TextUtils.isEmpty(iUserBankView.getBankPositive())) {
                iUserBankView.showT("请上传银行卡正面照片!");
                return;
            }
        }
        if (iUserBankView.getBankBack() == null || !iUserBankView.getBankBack().exists()) {
            if (TextUtils.isEmpty(iUserBankView.getBankBacks())) {
                iUserBankView.showT("请上传银行卡反面照片!");
                return;
            }

        }

        //所有验证通过
        String key1 = OssUtils.getInstance().objectKeyUserBankInfo + UserManager.getUserId() + "_" + iUserBankView.getIdFront().getName();
        String key2 = OssUtils.getInstance().objectKeyUserBankInfo + UserManager.getUserId() + "_" + iUserBankView.getIdBack().getName();
        String key3 = OssUtils.getInstance().objectKeyUserBankInfo + UserManager.getUserId() + "_" + iUserBankView.getBankFront().getName();
        String key4 = OssUtils.getInstance().objectKeyUserBankInfo + UserManager.getUserId() + "_" + iUserBankView.getBankBack().getName();

        iUserBankView.showLod("上传中...");
        OssUtils.getInstance().uploadfile(key1, iUserBankView.getIdFront().getAbsolutePath(), new UploadFileListener() {
            @Override
            public void onSucess(String path) {
                iUserBankView.closeLod();
                urlIdFront = path;
                addBankCard(bank_id);
            }

            @Override
            public void onFailure() {
            }

            @Override
            public void onProgress(long progress) {

            }
        });
        OssUtils.getInstance().uploadfile(key2, iUserBankView.getIdBack().getAbsolutePath(), new UploadFileListener() {
            @Override
            public void onSucess(String path) {
                iUserBankView.closeLod();
                urlIdBack = path;
                addBankCard(bank_id);
            }

            @Override
            public void onFailure() {

            }

            @Override
            public void onProgress(long progress) {

            }
        });
        OssUtils.getInstance().uploadfile(key3, iUserBankView.getBankFront().getAbsolutePath(), new UploadFileListener() {
            @Override
            public void onSucess(String path) {
                iUserBankView.closeLod();
                urlBankFront = path;
                addBankCard(bank_id);
            }

            @Override
            public void onFailure() {

            }

            @Override
            public void onProgress(long progress) {

            }
        });
        OssUtils.getInstance().uploadfile(key4, iUserBankView.getBankBack().getAbsolutePath(), new UploadFileListener() {
            @Override
            public void onSucess(String path) {
                iUserBankView.closeLod();
                urlBankBack = path;
                addBankCard(bank_id);
                iUserBankView.closeLod();
            }

            @Override
            public void onFailure() {

            }

            @Override
            public void onProgress(long progress) {

            }
        });
    }

    /**
     * 获取银行卡信息
     */
    public void getBankInfo() {
        iUserBankView.showLod("加载中...");
        HttpManager.getInstance().user_getBankCard(UserManager.getUserId(), new ResponseListener<MyBankInfoResultBean>() {
            @Override
            public void onSuccess(MyBankInfoResultBean myBankInfoResultBean) {
                iUserBankView.closeLod();
                iUserBankView.setBankInfo(myBankInfoResultBean);
            }

            @Override
            public void onFailure(String responseresult) {
                iUserBankView.closeLod();
                iUserBankView.showT(responseresult);
            }
        });
    }

    public void withdrawCash(long price) {
        iUserBankView.showLod("加载中...");
        HttpManager.getInstance().user_withdrawCash(UserManager.getUserId(), price, new ResponseListener<BaseBean>() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                iUserBankView.closeLod();
                iUserBankView.closeCurrentActivity();
            }

            @Override
            public void onFailure(String responseresult) {
                iUserBankView.closeLod();
                iUserBankView.showT(responseresult);
            }
        });
    }

    /**
     * 更新/添加 银行卡信息 到自己服务器
     *
     * @param bank_id
     */
    private void addBankCard(String bank_id) {
        if (urlIdFront != null && urlIdBack != null && urlBankFront != null && urlBankBack != null) {
            HttpManager.getInstance().user_updateBankCard(
                    bank_id,
                    UserManager.getUserId(),
                    iUserBankView.getUsername(),
                    iUserBankView.getBankNumber(),
                    urlIdFront,
                    urlIdBack,
                    urlBankFront,
                    urlBankBack,
                    iUserBankView.getBank(),
                    new ResponseListener<BaseBean>() {
                        @Override
                        public void onSuccess(BaseBean baseBean) {
                            L.e("添加信息成功","askdfk");
                            iUserBankView.closeLod();
                            iUserBankView.closeCurrentActivity();
                        }

                        @Override
                        public void onFailure(String responseresult) {
                            iUserBankView.closeLod();
                            iUserBankView.showT(responseresult);
                        }
                    }
            );
        } else {
            L.d("WJ", "图片未上传完毕");
        }
    }
}
